Skip to content

add a set of apis to configure wasi-nn via InstantiationArgs2#4764

Open
zhanheng1 wants to merge 28 commits intobytecodealliance:mainfrom
dongsheng28849455:InstantiationArgs2-wasinn
Open

add a set of apis to configure wasi-nn via InstantiationArgs2#4764
zhanheng1 wants to merge 28 commits intobytecodealliance:mainfrom
dongsheng28849455:InstantiationArgs2-wasinn

Conversation

@zhanheng1
Copy link
Contributor

add a set of apis to configure wasi-nn via InstantiationArgs2:

  1. Add a new command line option for wasi-nn: --wasi-nn-graph=encoding:target:model_path1:model_path2:...:model_pathN;
  2. Add new structs to manage this cmd option: WASINNGlobalContext and wasi_nn_graph_registry;
  3. Add 2 new parameters for load_by_name(encoding and target). load_by_name() can get encoding and target from WASINNGlobalContext and set into backend.
  4. Update test codes.

@zhanheng1 zhanheng1 force-pushed the InstantiationArgs2-wasinn branch from 77794ae to c73e4aa Compare December 19, 2025 07:07
@zhanheng1 zhanheng1 marked this pull request as ready for review December 23, 2025 01:52
#include "wasi_nn.h"

#undef WASM_ENABLE_WASI_EPHEMERAL_NN
#undef WASI_NN_NAME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

@zhanheng1 zhanheng1 Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In wasi_ephemeral_nn.h, macros are defined and then wasi_nn.h is included immediately after, but these macro definitions are later undefined (#undef). This means that in utils.h, the macros might be in an undefined state, leading to compilation errors( if WASM_ENABLE_WASI_EPHEMERAL_NN != 0).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasi_ephemeral_nn.h header is meant to be used by applications which uses this version of api.
these macros are not meant to be used by applications. (WASM_ENABLE_WASI_EPHEMERAL_NN, WASI_NN_NAME, WASI_NN_ERROR_TYPE, etc)
they are supposed to use eg. wasi_ephemeral_nn_error.
see samples like wamr-wasi-extensions/samples/nn-cli/main.c

/opt/wasi-sdk/bin/clang \
--target=wasm32-wasi \
-DWASM_ENABLE_WASI_NN=1 \
-DWASM_ENABLE_WASI_EPHEMERAL_NN=1 \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you enable WASM_ENABLE_WASI_EPHEMERAL_NN for this, but not for test_tensorflow_quantized.c the below?

have you run these tests at all?
if not, i'd suggest to leave these to use the legacy api for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you enable WASM_ENABLE_WASI_EPHEMERAL_NN for this, but not for test_tensorflow_quantized.c the below?

Just forgot to enable them for test_tensorflow_quantized.c, thanks.

have you run these tests at all?

Yes, I have tested them all with WASM_ENABLE_WASI_EPHEMERAL_NN enabled.

const uint32_t **encoding,
const uint32_t **target,
uint32_t n_graphs,
const char **graph_paths);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i suppose these should be in wasm_export.h as it's used by the embedding applications like iwasm.


WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_nn_registry(wasm_module_inst_t module_inst,
struct WASINNRegistry *wasi_ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasm_runtime_get_wasi_nn_registry/wasm_runtime_set_wasi_nn_registry are not supposed to be used by applications, are they?
i guess they should not be in wasm_export.h.

in wasm_export.h, put only api which is used directly by application. (eg. iwasm)
internal api should go to somewhere else, like wasm_runtime_common.h


WASM_RUNTIME_API_EXTERN void
wasm_runtime_set_wasi_nn_registry(WASMModuleInstanceCommon *module_inst_comm,
WASINNRegistry *wasi_nn_ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two duplicated wasm_runtime_set_wasi_nn_registry prototypes in this file.


#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
wasm_runtime_wasi_nn_registry_create(&nn_registry);
wasi_nn_set_init_args(inst_args, nn_registry, &wasi_nn_parse_ctx);
Copy link
Contributor

@yamt yamt Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasi_nn_set_init_args vs inst_args look a bit confusing.
maybe you meant wasi_nn_set_inst_args?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants